package com.example.deertrip;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.deertrip.bean.Flight;
import com.example.deertrip.database.DBOperate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Manage_flight_Model extends AppCompatActivity {
    private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    private ListView listView;
    SimpleAdapter adapter=null;
    private DBOperate dbOperate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage_flight_model);

        listView=findViewById(R.id.manage_flight_lv);
        dbOperate=new DBOperate(this);
        show();
    }

    private void show() {
        List<Flight> flights = dbOperate.get_All_Flight_Data();
        List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();//存放全部数据的列表
        Drawable dw;

        for (int i = 0; i < flights.size(); i++) {
            HashMap<String, Object> items = new HashMap<String, Object>(); //存放一条数据的哈希表
            Flight flight = flights.get(i);
            BitmapDrawable bd = new BitmapDrawable(this.getResources(), flight.getCompanyImage());//将Test对象中读取的点阵图片转换为bitmapdrawable
            dw = bd;    //再转换为Drawable
            items.put("CompanyImage", dw);   //属性分别存入哈希表
            items.put("FlightNumber", flight.getFlightNumber());
            items.put("TakeoffTime", flight.getTakeoffTime());
            items.put("LandingTime", flight.getLandingTime());
            items.put("FlightForm", flight.getFlightForm());
            items.put("FlightTo", flight.getFlightTo());
            items.put("TicketPrice", flight.getTicketPrice());
            items.put("TicketPrice", flight.getTicketPrice());
            items.put("TotalHour", flight.getTotalHour());
            items.put("CompanyName", flight.getCompanyName());
            data.add(items);   //总数据哈希表中加入该条数据项
        }


        adapter = new SimpleAdapter(this, data, R.layout.flight_info_bar_delete,
                new String[]{"CompanyImage", "FlightNumber", "TakeoffTime", "LandingTime", "FlightForm", "FlightTo"}, new int[]{R.id.iv_1, R.id.tv_5, R.id.tv_1, R.id.tv_2, R.id.tv_3, R.id.tv_4}){
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                final int p = position;
                Button deletebtn;// id为你自定义布局中按钮的id
                convertView = super.getView(position, convertView, parent);
                deletebtn = convertView.findViewById(R.id.flight_delete);
                deletebtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        HashMap<String, Object> items = new HashMap<String, Object>();
                        items = (HashMap<String, Object>) getItem(p);
                        Integer id = (Integer) items.get("FlightNumber");
                        System.out.println(id);
                        System.out.println("点击了第" + p + "项");
                        dbOperate.delete_flight(id);
                        data.remove(p);
                        adapter.notifyDataSetChanged();
                        Log.i("delete", "删除成功");
                        Toast.makeText(Manage_flight_Model.this, "删除成功！", Toast.LENGTH_LONG).show();
                    }
                });

                return convertView;
            }
        };

        adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
            public boolean setViewValue(View view, Object data, String textRepresentation) {
                if (view instanceof ImageView && data instanceof Drawable) {
                    ImageView iv = (ImageView) view;
                    iv.setImageDrawable((Drawable) data);
                    return true;
                } else return false;
            }
        });

        listView.setAdapter(adapter);
    }
}